import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const enabledData = [{ name:'待派送', value:1 },{ name:'已送达', value:3 }]
// ===================================================================== component
class Index extends React.Component{
	state = {	
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '条码号',		name: 'spec_code', onValue:({ rows }) => {
				this.model.outsourcing_status = rows ? rows.name : ''
			} },
			{ label: '派送状态',		name: 'outsourcing_status', type:'select' },
			{ label: '接收时间',  	names: ['start_date','end_date'], type:'date-range' },
		],
		
	}
	query = {}
	model = {}

	
	componentDidMount(){
		const empl_id = $fn.getUser().uuid
		this.query = {outsourcing_type:'',empl_id:empl_id}
		this.getSelect()
		if(window.isInit){
		  this.fetch()
		}
	}
	// 获取下拉数据
	 getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 派送状态
			$fn.setRow( forms, 'outsourcing_status', await f.outsourcing_state )
			//
			this.setState({ forms })
		})
	 }
	
	// paging
	fetch = () => $fn.fetch.call(this,'specimen/myOutsourcingList', { isIndex:true })
	
	// 表格
	cols = [
		{ title: '外送单位', 		dataIndex: 'outsourcing_company', 	width:200 },
		{ title: '接收时间', 		dataIndex: 'created_at', 			...$fn.table.time },
		{ title: '条码',				dataIndex: 'spec_code', 			...$fn.table.spec_code },
		{ title: '状态',				dataIndex: 'outsourcing_status', 	width:200,  render: text=>$fn.getState(enabledData,text)  },
		{ title: '姓名',				dataIndex: 'patient_name', 			...$fn.table.name },
		{ title: '性别', 			dataIndex: 'sex_name', 				...$fn.table.sex },
		{ title: '年龄', 			dataIndex: 'age', 					...$fn.table.age},
		{ title: '自然项目',			dataIndex: 'parent_kind_name', 		width:200 },
	]
	// 操作按钮
	controls = () => [
		Confirm.call(this, '已送达', 'specimen/changeOutsourcingStatus',{ isKeys:true, ghoust:true, query:(keys,rows)=>{
			const spec_code = []
			rows.forEach( (v) => {
				spec_code.push(v.spec_code)
			} )
			return{outsourcing_status:3,spec_code}
		} } ),
		Controls.exports.call(this, 'specimen/myOutsourcingList'),			// 导出	
	]
	
	render(){
		return (
			<>
				<Page title='我的外包清单' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isIndex:true, isInit: window.isInit }) }
				</Page>
				
			</>
		)
	}
}

export default Index